﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using Codeplex.Data;
using System.Security.Principal;

namespace BitcoinStats
{
    public partial class Form1 : Form
    {
        bool loaded { get; set; }
        string usd { get; set; }
        string eur { get; set; }
        string cad { get; set; }
        string gbp { get; set; }
        bool price_notify { get; set; }
        bool taskbar { get; set; }
        int width { get; set; }
        int height { get; set; }
        string readed { get; set; }
        string readed2 { get; set; }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) { initalize(); }

        public void initalize()
        {
            bool isElevated;
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
            if(!isElevated) { MessageBox.Show("You must run this app with administrator rights!"); this.Close(); }
            loaded = false;
            if(!System.IO.Directory.Exists("C:/BitcoinStats/")) { System.IO.Directory.CreateDirectory("C:/BitcoinStats/"); }
            if(!System.IO.File.Exists("C:/BitcoinStats/config.btc")) {
                var writer = new System.IO.StreamWriter("C:/BitcoinStats/config.btc");
                writer.Write("false");
                writer.Close();
            }

            if (!System.IO.File.Exists("C:/BitcoinStats/config2.btc"))
            {
                var writer2 = new System.IO.StreamWriter("C:/BitcoinStats/config2.btc");
                writer2.Write("false");
                writer2.Close();
            }
            var reader = new System.IO.StreamReader("C:/BitcoinStats/config.btc");
            readed = reader.ReadToEnd();
            reader.Close();

            var reader2 = new System.IO.StreamReader("C:/BitcoinStats/config2.btc");
            readed2 = reader2.ReadToEnd();
            reader2.Close();

            if (readed == "true") { taskbar = true; checkBox2.Checked = true; } else { taskbar = false; checkBox2.Checked = false; }
            if (readed2 == "true") { price_notify = true; checkBox1.Checked = true; } else { price_notify = false; checkBox1.Checked = false; }
            taskbar = checkBox2.Checked;
            timer1.Start();
        }

        public void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
        {
            if(!loaded)
            {
                var wc = new WebClient();
                var json_response = wc.DownloadString("http://www.blockchain.info/ticker");
                var json = DynamicJson.Parse(@json_response);
                usd = json.USD.last.ToString();
                eur = json.EUR.last.ToString();
                cad = json.CAD.last.ToString();
                gbp = json.GBP.last.ToString();
                loaded = true;
            }

            string currency = domainUpDown1.SelectedItem.ToString();
            switch (currency)
            {
                case "USD":
                    label3.Text = usd + " USD";
                    break;
                case "EUR":
                    label3.Text = eur + " EUR";
                    break;
                case "CAD":
                    label3.Text = cad + " CAD";
                    break;
                case "GBP":
                    label3.Text = gbp + " GBP";
                    break;
                default:
                    MessageBox.Show("ERROR: Unleash Ninja Guru");
                    break;
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text != "")
            {
                var wc = new WebClient();
                try {
                    var json_response = wc.DownloadString("http://www.blockchain.info/address/" + textBox1.Text + "/?format=json");
                    var json = DynamicJson.Parse(@json_response);
                    var final_balance = json.final_balance / 100000000;
                    var total_received = json.total_received / 100000000;
                    var transactions = json.n_tx;
                    final.Text = final_balance.ToString() + " BTC";
                    receive.Text = total_received.ToString() + " BTC";
                    transaction.Text = transactions.ToString();
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Enter a valid address!");
                }
                //var json = DynamicJson.Parse(@json_response);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(price_notify) { price_notify = false; notifyIcon1.ShowBalloonTip(1); } else { price_notify = true; notifyIcon1.ShowBalloonTip(1); }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (taskbar) { taskbar = false; notifyIcon1.ShowBalloonTip(1); } else { taskbar = true; notifyIcon1.ShowBalloonTip(1); }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            var wc = new WebClient();
            var json_response2 = wc.DownloadString("http://www.blockchain.info/ticker");
            var json = DynamicJson.Parse(@json_response2);
            if(price_notify) {
                notifyIcon1.BalloonTipText = json.USD.last + " USD";
                notifyIcon1.ShowBalloonTip(1);
            }
            timer1.Start();
        }

        public void taskbar_minimize()
        {
            this.FormBorderStyle = FormBorderStyle.None;
            height = this.Height;
            width = this.Width;
            this.Width = 0;
            this.Height = 0;
            this.ShowInTaskbar = false;
            this.Visible = false;
        }
        public void taskbar_show()
        {
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            this.Width = 695;
            this.Height = 280;
            this.ShowInTaskbar = true;
            this.Visible = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
           if(taskbar) {
                e.Cancel = true;
                taskbar_minimize();
            }
           if(checkBox2.Checked == true) {
                System.IO.File.Delete("C:/BitcoinStats/config.btc");
                var writer = new System.IO.StreamWriter("C:/BitcoinStats/config.btc");
                writer.Write("true");
                writer.Close();
            } else
            {
                System.IO.File.Delete("C:/BitcoinStats/config.btc");
                var writer = new System.IO.StreamWriter("C:/BitcoinStats/config.btc");
                writer.Write("false");
                writer.Close();
            }

            if(checkBox1.Checked == true) {
                System.IO.File.Delete("C:/BitcoinStats/config2.btc");
                var writer = new System.IO.StreamWriter("C:/BitcoinStats/config2.btc");
                writer.Write("true");
                writer.Close();
            } else
            {
                System.IO.File.Delete("C:/BitcoinStats/config2.btc");
                var writer = new System.IO.StreamWriter("C:/BitcoinStats/config2.btc");
                writer.Write("false");
                writer.Close();
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            taskbar_show();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            taskbar = false;
            this.Close();
        }
    }
}


